package com.oocl;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * java中调用js代码
 * @author JasonWu
 * @version
 */
public class ScriptEngineTest {
	public static void main(String[] args) {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		try {
			engine.eval("var a=1;var b=2;print(a+b);");
			
			// 无法调用浏览器中定义的JS函数，会有ReferenceError异常
//			engine.eval("alert(\"hello JS\");");
		} catch (ScriptException e) {
			e.printStackTrace();
		}
	}

}
